package org.NooLab.itexx.co;

import java.util.ArrayList;

import org.NooLab.itexx.ITexx;
import org.NooLab.itexx.co.targets.StarterIntf;
import org.NooLab.itexx.comm.tcp.box.infra.DataSenderIntf;
import org.NooLab.utilities.logging.PrintLog;




public class ExtWorker {

	
	ExtWorkers extWorkers;
	ITexx itexx;
	
	StarterIntf starter ;
	
	public String instanceGuid = "";
	public String appWorkingDir;
	public String jarFilename;

	 
	int proposedPort =0;
	int effectiveClientPort = 0;
	
	int status = CoWorkerIntf._CO_STATUS_UNKNOWN ;
	long startingSince = 0L;
	
	DataSenderIntf sender = null;

	
	ArrayList<String> arguments = new ArrayList<String> (); 
	
	int exitValue;
	private boolean created;
	String name;
	private String jarFilePath;
	
	
	PrintLog out;
	// ========================================================================
	public ExtWorker(ExtWorkers xworkers, ITexx itx) {
	
		 extWorkers = xworkers;
		 itexx = itx;
		 
		 this.out = xworkers.out;
	}
	// ========================================================================
	

	public void close() {
		
		arguments.clear();
	}


	class Launcher implements Runnable{

		
		public Launcher(){
			
		}
		
		public void start(){
			
		}
		@Override
		public void run() {
			 
			try {
				
				launch() ;
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	} // class 
	
	public void launch() throws Exception{
		
		
		starter.startingCall( this ) ;
		
		if (created ==false){
			return;
		}
		  
	}

	
	/**
	 * returns the complete argument as defined in processDef (type = ExtWorker)
	 * 
	 * @param string
	 * @return
	 */
	public String getArgumentByTopic(String topic) {
		String completeArg="" ;
		
		for (int i=0;i<arguments.size();i++){
			String str = arguments.get(i) ;
			
			if (str.startsWith(topic.trim())){
				completeArg = str ;  
				break;
			}
			
		}//i->
		
		return completeArg;
	}
	
	
	
	private String getArgumentValueStr( ArrayList<String> args, String marker) {
	
		String avStr="",str;
		
		for (int i=0;i<args.size();i++){
			if (args.get(i).trim().startsWith(marker)){
				str = args.get(i).trim();
				if (str.indexOf(":")>2){
					avStr = str.split(":")[1];
				}
				break;
			}
		}
		return avStr;
	}

	
	public void setStarterMethod(StarterIntf starter) {
		this.starter = starter;		
	}


	public void setGuid(String guid) {
		// TODO Auto-generated method stub
		
	}

	public String getName() {
		return name;
	}

	public String getInstanceGuid() {
		return instanceGuid;
	}

	public void setInstanceGuid(String instanceGuid) {
		this.instanceGuid = instanceGuid;
	}

	public String getAppWorkingDir() {
		return appWorkingDir;
	}

	public void setAppWorkingDir(String appWorkingDir) {
		this.appWorkingDir = appWorkingDir;
	}

	public String getJarFilename() {
		return jarFilename;
	}

	public void setJarFilename(String jarFilename) {
		this.jarFilename = jarFilename;
	}

	public int getProposedPort() {
		return proposedPort;
	}

	public void setProposedPort(int proposedPort) {
		this.proposedPort = proposedPort;
	}

	public int getEffectiveClientPort() {
		return effectiveClientPort;
	}

	public void setEffectiveClientPort(int effectiveClientPort) {
		this.effectiveClientPort = effectiveClientPort;
	}


	public int getStatus() {
		return status;
	}


	public void setStatus(int status) {
		this.status = status;
	}


	public long getStartingSince() {
		return startingSince;
	}


	public void setStartingSince(long startingSince) {
		this.startingSince = startingSince;
	}

	public ArrayList<String> getArguments() {
		return arguments;
	}


	public void setArguments(ArrayList<String> arguments) {
		this.arguments = arguments;
	}




	public int getExitValue() {
		return exitValue;
	}




	public void setExitValue(int exitValue) {
		this.exitValue = exitValue;
	}




	public void showArguments() {
		
		out.print(2, "\nLaunching ExtWorker <"+name+"> with arguments ...");
		
		for (int i=0;i<arguments.size();i++){
			out.print(2, "   "+arguments.get(i));
		}// i->
		
		out.print(2, "\n\n");
	}


	public void setJarFilePath(String path) {
		jarFilePath = path;
	}


	public void addArgument(String arg) {
		arguments.add(arg);
	}


	public void setName(String _name) {
		name = _name;
	}

 

}
